package net.arvin.mobilebanksystem.fragment;

import java.util.ArrayList;
import java.util.List;

import net.arvin.dao.LatestTransferAdapter;
import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.MainActivity;
import net.arvin.model.TransfersBill;
import net.arvin.service.TransfersService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

public class TransferFragment extends Fragment implements OnClickListener,
		OnItemClickListener {
	private RelativeLayout transfer2users;
	private RelativeLayout transfer2card;
	private ListView latest_transfer;

	private ResponseHandler mResponseHandler;
	private List<TransfersBill> mBills;
	private LatestTransferAdapter transfersBillAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_transfer, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
		initData();
		initListener();
		initListView();
	}

	private void initData() {
		String data = PreferenceUtils.getPreferTransfersBillJson(getActivity());
		if (data == null || data.equals("")) {
			mBills = new ArrayList<TransfersBill>();
		} else {
			mBills = JSON.parseArray(data, TransfersBill.class);
		}
	}

	private void initListView() {
		transfersBillAdapter = new LatestTransferAdapter(mBills, getActivity());
		latest_transfer.setAdapter(transfersBillAdapter);
		latest_transfer.setOnItemClickListener(this);
		if (NetConnectionUtil.isConnectWithTip(getActivity(), "没有联网=_=!")) {
			TransfersService.tryGetAllTransfer(
					PreferenceUtils.getPreferPhonenumber(getActivity()),
					getResponseHandler(), getActivity());
		}
	}

	private void initListener() {
		transfer2users.setOnClickListener(this);
		transfer2card.setOnClickListener(this);
	}

	private void initView(View view) {
		MainActivity.mainActivity.setTitleTv("转账汇款");
		transfer2users = (RelativeLayout) view
				.findViewById(R.id.transfer2users);
		transfer2card = (RelativeLayout) view.findViewById(R.id.transfer2card);
		latest_transfer = (ListView) view.findViewById(R.id.latest_transfer);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		MainActivity.mainActivity.setTitleTv("转账详情");
		TransferDetailsFragment fragment = new TransferDetailsFragment(mBills.get(position));
		MainActivity.mainActivity.switchContent(fragment, false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.transfer2users:
			MainActivity.mainActivity.setTitleTv("转到手机银行账户");
			MainActivity.mainActivity.switchContent(new TransferUserFragment(),
					false);
			break;
		case R.id.transfer2card:
			MainActivity.mainActivity.setTitleTv("转到银行卡");
			MainActivity.mainActivity.switchContent(new TransferCardFragment(),
					false);
			break;
		}
	}

	private ResponseHandler getResponseHandler() {
		if (mResponseHandler == null) {
			mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					mBills = JSON.parseArray(data.toString(),
							TransfersBill.class);
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.TransfersBill, data.toString(),
							getActivity());
					transfersBillAdapter = new LatestTransferAdapter(mBills,
							getActivity());
					latest_transfer.setAdapter(transfersBillAdapter);
				}

				@Override
				public void onRequestFailed(final Object data) {
					if (data.toString().equals("user")) {
						Toast.makeText(getActivity(), "转入账户不存在=_=!",
								Toast.LENGTH_SHORT).show();
					}
				}
			};
		}
		return mResponseHandler;
	}
}
